package com.zlq.login.controllermvc;

import com.google.code.kaptcha.Constants;
import com.zlq.login.dao.UserDaoLo;
import com.zlq.login.entity.User;
import com.zlq.login.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@Controller
public class toLoginController {
    @Resource
    private UserService userService;
    //Request processing failed; nested exception is java.lang.ClassCastException:
    // java.util.ArrayList cannot be cast to com.zlq.login.entity.User

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(User user, String code, HttpServletRequest req) throws Exception{
        String scode = (String) req.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
        String name = user.getName();
        String password = user.getPassword();
        if (code != null) {
            if (scode.equals(code)) {
                if (name != null && !name.equals("")) {
                    User user1 = userService.selectByName(name);
                    if (user1.getName().equals(name) && user1.getPassword().equals(password)) {
                            return "index";
                        }

                } else {
                    return "login";
                }

            } else {
                return "login";
            }

        } else {
            return "login";
        }
        return null;

    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    @RequestMapping("/register")
    public String register(User user, String code, HttpSession session, HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String token = (String) req.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        req.getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
        String name = user.getName();
        String password = user.getPassword();
        if ((!name.isEmpty() || !name.equals("")) && (!password.isEmpty() || !password.equals("")) && (code != null ||token.equals(code))){
            req.getSession().setAttribute("name",name);
            userService.save(user);
            return "login";
        }else{

            return "register";
        }
    }

    @PostMapping("/userNameVerify")
    @ResponseBody
    public Map UserNameVerify(@RequestBody User user){
        String name = user.getName();
        System.out.println(name);
        User user1 = userService.selectByName(name);
        System.out.println(user);
        ModelMap map = new ModelMap();
        if (user1 != null){
            map.put("result",false);
        }else {
            map.put("result",true);
        }
       return map;

    }


}
